package com.common.server.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.common.server.common.ErrorCode;
import com.common.server.constant.UserConstant;
import com.common.server.entity.base.BaseEntity;
import com.common.server.entity.user.UserInfo;
import com.common.server.exception.ThrowUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Date;
import java.util.Objects;

/**
 * ClassName: DefaultDBFieldHandler
 * Package: com.common.server.config
 * Description:
 *
 * @Author Memory
 * @Create 2025/1/2 14:10
 * @Version 1.0
 */

/**
 * 公共属性字段值填充
 */
public class DefaultDBFieldHandler implements MetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        if (Objects.nonNull(metaObject) && metaObject.getOriginalObject() instanceof BaseEntity) {
            // 填充创建时间
            metaObject.setValue("createTime", LocalDateTime.now());
            // 填充修改时间
            metaObject.setValue("updateTime", LocalDateTime.now());
            // 获取当前登录用户
            UserInfo currentUser = getCurrentUser();
            if (Objects.nonNull(currentUser)) {
                // 填充创建人信息
                metaObject.setValue("createBy", currentUser.getId());
                metaObject.setValue("updateBy", currentUser.getId());
            } else {
                metaObject.setValue("createBy", 1L);
                metaObject.setValue("updateBy", 1L);
            }
        }
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 填充更新时间
        metaObject.setValue("updateTime", LocalDateTime.now());
        // 获取当前登录用户
        UserInfo currentUser = getCurrentUser();
        // 填充修改人信息
        if (Objects.nonNull(currentUser)) {
            metaObject.setValue("updateBy", currentUser.getId());
        } else {
            metaObject.setValue("updateBy", 1L);
        }
    }

    /**
     * 获取当前登录用户
     *
     * @return
     */
    private UserInfo getCurrentUser() {
        // 先判断是否已登录
        HttpServletRequest request =
                ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder
                        .getRequestAttributes())).getRequest();
        return (UserInfo) request.getSession().getAttribute(UserConstant.USER_LOGIN_STATE);
    }

    /**
     * 将 Date 转换为 LocalDateTime，使用系统默认时区
     */
    private LocalDateTime convertDateToLocalDateTime(Date date) {
        // 将 Date 转换为 Instant
        Instant instant = date.toInstant();
        // 使用系统默认时区将 Instant 转换为 ZonedDateTime
        ZonedDateTime zonedDateTime = instant.atZone(ZoneId.systemDefault());
        // 从 ZonedDateTime 中提取 LocalDateTime
        return zonedDateTime.toLocalDateTime();
    }
}
